package com.neusoft.demo12;

import java.sql.*;

public class JdbcPrepareStatementDemo {
    public static void main(String[] args) {

        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获取数据库的连接
            Connection conn = DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/mysq_dbtest","root","123456");
            //3. 获取PreparedStatement对象用于执行动态的SQL语句
            PreparedStatement pstmt = conn.
                    prepareStatement("select * from user where username=? and password=?");
            pstmt.setString(1,"xxx"); //将？替换为具体的值
            pstmt.setString(2,"111111");
            //pstmt.executeUpdate();// 执行更新的操作
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString(1));
                System.out.println(rs.getString(2));
            }
            //释放资源
            rs.close();
            pstmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
